//
//  WBStatusDetailSectionHeaderView.m
//  WeiBo
//
//  Created by bao on 2017/10/9.
//  Copyright © 2017年 powertorque. All rights reserved.
//

#import "WBStatusDetailSectionHeaderView.h"

#import "WBStatus.h"

#define kMargin 12.5f

@interface WBStatusDetailSectionHeaderView ()

@property (nonatomic, weak) WBStatusDetailSectionButton *selectedButton;

@end

@implementation WBStatusDetailSectionHeaderView

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
        
        WBStatusDetailSectionButton *reweetedButton = [[WBStatusDetailSectionButton alloc] init];
        [self.contentView addSubview:reweetedButton];
        _reweetedButton = reweetedButton;
        
        WBStatusDetailSectionButton *commentButton = [[WBStatusDetailSectionButton alloc] init];
        [self.contentView addSubview:commentButton];
        _commentButton = commentButton;
        
        WBStatusDetailSectionButton *praiseButton = [[WBStatusDetailSectionButton alloc] init];
        [self.contentView addSubview:praiseButton];
        _praiseButton = praiseButton;
        
        UIView *indicateView = [[UIView alloc] init];
        indicateView.backgroundColor = kCSSHexColor(@"#FF8200");
        indicateView.frame = CGRectMake(0, kWBStatusDetailSectionHeaderViewHeight - 3 - 3, 25, 3);
        indicateView.layer.cornerRadius = 1.5;
        [self.contentView addSubview:indicateView];
        _indicateView = indicateView;
        
        [self.reweetedButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        [self.commentButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        [self.praiseButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}

- (void)click:(WBStatusDetailSectionButton *)button
{
    if (button == self.selectedButton) return;
    
    button.selected = YES;
    self.selectedButton.selected = NO;
    self.selectedButton = button;
    
    [UIView animateWithDuration:0.15 animations:^{
        self.indicateView.centerX = self.selectedButton.centerX;
    }];
    
    if ([self.delegate respondsToSelector:@selector(statusDetailSectionHeaderView:didClickButtonType:)]) {
        WBStatusDetailSectionHeaderViewClickType type = 0;
        if (button == self.reweetedButton) {
            type = WBStatusDetailSectionHeaderViewClickType_Reweeted;
        } else if (button == self.commentButton) {
            type = WBStatusDetailSectionHeaderViewClickType_Comment;
        } else {
            type = WBStatusDetailSectionHeaderViewClickType_Praise;
        }
        [self.delegate statusDetailSectionHeaderView:self didClickButtonType:type];
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    NSString *reweetedString = self.reweetedButton.titleLabel.text;
    NSString *commentString = self.commentButton.titleLabel.text;
    NSString *praiseString = self.praiseButton.titleLabel.text;
    
    UIFont *reweetedFont = self.reweetedButton.titleLabel.font;
    UIFont *commentFont = self.commentButton.titleLabel.font;
    UIFont *praiseFont = self.praiseButton.titleLabel.font;
    
    CGSize reweetedSize = [reweetedString sizeWithAttributes:@{NSFontAttributeName : reweetedFont}];
    CGSize commentSize = [commentString sizeWithAttributes:@{NSFontAttributeName : commentFont}];
    CGSize praiseSize = [praiseString sizeWithAttributes:@{NSFontAttributeName : praiseFont}];
    
    self.reweetedButton.frame = CGRectMake(kMargin, 0, reweetedSize.width, self.height);
    self.commentButton.frame = CGRectMake(CGRectGetMaxX(self.reweetedButton.frame) + kMargin, 0, commentSize.width, self.height);
    self.praiseButton.frame = CGRectMake(self.width - kMargin - praiseSize.width, 0, praiseSize.width, self.height);
    
    if (self.selectedButton) {
        self.indicateView.centerX = self.selectedButton.centerX;
    } else {
        self.indicateView.centerX = 0;
    }
}

#pragma mark - Setter

- (void)setStatus:(WBStatus *)status
{
    _status = status;
    
    NSString *reweetedString = [NSString stringWithFormat:@"转发 %ld", status.reposts_count.integerValue];
    NSString *commentString = [NSString stringWithFormat:@"评论 %ld", status.comments_count.integerValue];
    NSString *praiseString = [NSString stringWithFormat:@"赞 %ld", status.attitudes_count.integerValue];
    
    [self.reweetedButton setTitle:reweetedString forState:UIControlStateNormal];
    [self.commentButton setTitle:commentString forState:UIControlStateNormal];
    [self.praiseButton setTitle:praiseString forState:UIControlStateNormal];
    
    [self setNeedsLayout];
}

- (void)setDelegate:(id<kWBStatusDetailSectionHeaderViewDelegate>)delegate
{
    _delegate = delegate;
    
    [self click:self.commentButton];
}

@end


@implementation WBStatusDetailSectionButton

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [self setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
        self.titleLabel.font = [UIFont boldSystemFontOfSize:15];
    }
    return self;
}

- (void)setHighlighted:(BOOL)highlighted {}

@end
